package com.logic.sysHandler;

import java.sql.Timestamp;

import org.apache.log4j.Logger;
import org.jooq.SQLDialect;
import org.jooq.impl.Factory;

import com.DBObjects.dbo.Tables;
import com.DBObjects.dbo.tables.records.SysLogRecord;
import com.google.gson.JsonObject;

import utl.mofen.dbHandler.DBBaseHandler;
import utl.mofen.utils.StringUtil;

public class LoggerHandler extends DBBaseHandler {
	private final Logger logger = Logger.getLogger(this.getClass());
	
   
	public void doAction() throws Exception {	
		
		if (!config.has("data"))
		{
			return;			
		}
		
		String key = null;
		
		JsonObject logData = config.get("data").getAsJsonObject();
		
		Factory create = new Factory(this.dbOpera.getConnection(),
				SQLDialect.SQLSERVER);	
		
		//添加
		SysLogRecord r = create.newRecord(Tables.SYS_LOG);
		if (logData.has("msg"))
		{
			r.setFcontent(logData.get("msg").getAsString());
		}
		
		if (logData.has("infokey"))
		{
			key = logData.get("infokey").getAsString();
			if (StringUtil.stringIsNotEmpty(key))
			{
				r.setFinfokey(key);
			}
		}
		
		if (logData.has("info"))
		{
			r.setFinfo(logData.get("info").getAsString());
		}
		
		if (logData.has("userkey"))
		{
			String userkey = logData.get("userkey").getAsString();
			if (StringUtil.stringIsNotEmpty(userkey))
			{
				r.setFuserkey(userkey);
			}
		}
		
		if (logData.has("username"))
		{
			r.setFusername(logData.get("username").getAsString());
		}
		
		if (logData.has("modulecaption"))
		{
			r.setFmodulecaption(logData.get("modulecaption").getAsString());
		}
		
		if (logData.has("moduleindex"))
		{
			r.setFmoduleindex(logData.get("moduleindex").getAsString());
		}
		
		if (logData.has("host"))
		{
			r.setFhost(logData.get("host").getAsString());
		}
		
		
		r.setFtime(new Timestamp(System.currentTimeMillis()));
		
		r.store();		
	}
}
